import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { FormInstance } from 'antd/lib/form';
import { Modal, Button, Form, message, Input,} from 'antd';
import PropTypes from "prop-types";

const FormItem = Form.Item;
const mapStateToProps = (state: any) => ({
  HandsetMng: state.HandsetMng,
})
class HandleModal extends PureComponent<any, any> {
  formRef = React.createRef<FormInstance>();
  static contextTypes = {
    permissions: PropTypes.array,
    dicData: PropTypes.object,
  };

  constructor(props: any) {
    super(props);
    this.state = {
      loading: false,
    };
  }

  componentDidUpdate(prevProps: any) {
    const { modalVisible, formData } = this.props
    if (modalVisible && modalVisible !== prevProps.modalVisible && formData) {
      this.formRef && this.formRef.current?.setFieldsValue({
        ...formData
      })
    }
  }

  handleSubmit = () => {
    this.formRef.current?.submit();
  }

  onFinish = (values: any) => {
    const { dispatch, formData, freshPage } = this.props
    let postData = {
      ...values,
    }
    let dType = 'HandsetMng/postObjWithRes'
    let url = '/msalcohollock/handhelds' //新增
    if (formData?.sid) {
      dType = 'HandsetMng/putObjWithRes'
      url = `/msalcohollock/handhelds/${formData?.sid}` //编辑
    }
    this.setState({
      loading: true
    })
    dispatch({
      type: dType,
      payload: {
        url: url,
        ...postData,
      },
      callback: (res: any) => {
        if (res.statusCode === '0') {
          message.success( formData?.sid ? '编辑成功': '新增成功');
          this.close();
          freshPage()
        }
        this.setState({
          loading: false
        })
      }
    })
  }

  close = () => {
    const { closeModal } = this.props;
    this.formRef.current && this.formRef.current.resetFields();
    closeModal();
  }

  render() {
    const { formData, modalVisible } = this.props
    const { loading } = this.state
    const footer = (
      <div>
        <Button size="small" onClick={this.close}>
          取消
        </Button>
        <Button size="small" loading={loading} onClick={this.handleSubmit} type="primary">
          确定
        </Button>
      </div>
    )
    let title = formData.sid ? '编辑手持机' : '新增手持机';

    const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { 
          span: 6,
          // offset: 4,
        },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { 
          span: 14,
        },
        md: { span: 16 },
      },
    };

    return (
      <Modal
        title={title}
        footer={footer}
        visible={modalVisible}
        width={700}
        onCancel={this.close}
      >
        <Form
          style={{ marginTop: 12 }}
          ref={this.formRef}
          onFinish={this.onFinish}
        >
          <FormItem
            {...formItemLayout}
            label="手持机编号"
            colon={false}
            name="code"
            rules={[{ required: true, message: '请输入手持机编号,只能输入英文及数字', whitespace: true, pattern: /^[A-Za-z0-9]+$/, }]}
          >
            <Input placeholder="请输入手持机编号" maxLength={50} disabled={formData.sid}/>
          </FormItem>
          <FormItem
            {...formItemLayout}
            label="手持机名称"
            colon={false}
            name="name"
            // rules={[{required: false, message: '只能输入中英文、数字及特殊字符', pattern: /^[\u4e00-\u9fa5a-zA-Z0-9\s\w]+$/,}]}
            rules={[{required: false, message: '不能输入空字符', pattern: /^\S+$/,}]}
          >
            <Input placeholder="请输入手持机名称" maxLength={50}/>
          </FormItem>
        </Form>
      </Modal>
    )
  }
}
export default connect(mapStateToProps)(HandleModal);
